#include <cmath>
#include "Neuron.h"

namespace fullsail_ai {

	void Neuron::initialize(std::size_t argumentCount, int (*numberGenerator)())
	{
		// TODO: 4
		// Initialize the bias and the weights to random values.
		double num = (double)numberGenerator()/RAND_MAX;
		bias = num - .5f;

		for(unsigned int i = 0; i < argumentCount;++i)
		{
			double wght = (double)numberGenerator()/RAND_MAX;

			weights.push_back(wght - .5f);
		}
		//computeActivationValue(weights);
	}

	double Neuron::computeActivationValue(std::vector<double> const& arguments) const
	{
		// TODO: 6
		// Return the neuron's activation value based on the specified arguments.
		double sum = 0;
		for(unsigned int i = 0; i < arguments.size();++i)
		{
			sum += arguments[i] * weights[i];
		}
		
		sum += bias;
		
		return (1/(1+exp(-sum)));
	}

	void Neuron::adjust(std::vector<double> const& arguments, double valueError,
	                    double learningFactor)
	{
		// TODO: 11
		// Adjust each weight of the neuron, including the bias.
		for(unsigned int i = 0; i < weights.size();++i)
		{
			weights[i] += learningFactor * arguments[i] * valueError;
		}
		bias += learningFactor * 1.0f * valueError;
	}
}  // namespace fullsail_ai

